<?php

class Page{
	public $total; //总数
	public $length; //每页个数
	public $pagenum; // 总页数
	public $page; //当前页数
	public $offset;
	public $limit;
	public $prevpage; //上一页
	public $nextpage; //下一页
	
	// 构造函数
	function __construct($total,$length){
		$this->total=$total;
		$this->length=$length;
		$this->page=isset($_GET['p'])?$_GET['p']:1;
		$this->pagenum=ceil($this->total/$this->length);
		$this->offset=($this->page-1)*$this->length;
		$this->limit="limit {$this->offset},{$this->length}";
		$this->prevpage();
		$this->nextpage();
	}

	// 上一页计算
	function prevpage(){
		if($this->page<=1){
			$this->prevpage=1;
		}else{
			$this->prevpage=$this->page-1;
		}
	}

	// 下一页计算
	function nextpage(){
		if($this->page>=$this->pagenum){
			$this->nextpage=$this->pagenum;
		}else{
			$this->nextpage=$this->page+1;
		}
	}

	// 显示上一页下一页
	function show(){
		echo "<p><a href='index.php?p=1'>首页</a>|<a href='index.php?p={$this->prevpage}'>上一页</a>|<a href='index.php?p={$this->nextpage}'>下一页</a>|<a href='index.php?p={$this->pagenum}'>末页</a> </p>";

	}

}

?>